<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入编号"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="模型名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入模型名称"></a-input>
            </a-form-model-item>
          </a-col>
          <!--   <a-col :span="24">
          <a-form-model-item label="模型分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="model_base_type" placeholder="请选择类型" />
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="24">
            <a-form-model-item label="槽号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="slotId">
              <a-input v-model="model.slotId" placeholder="请输入槽号"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="数据集" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dataSetCode">
              <j-dict-select-tag
                type="list"
                v-model="model.dataSetCode"
                dictCode="tb_data_set,name,code"
                placeholder="请选择数据集"
                 @change="handleChangeDataSet" 
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="预测目标" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="predictProperty">
              <!-- <a-select default-value="电压" v-model="model.predictProperty" placeholder="请选择预测目标" >
                  <a-select-option value="电压">电压</a-select-option>
                  <a-select-option value="电流">电流</a-select-option>
              </a-select> -->
              <a-input-search
                placeholder="请选择预测目标"
                readOnly
                v-model="model.predictPropertyText"
                @search="doSelectDataPropertyList"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dataType">
              <a-select
                default-value="原始数据"
                v-model="model.dataType"
                placeholder="请选择数据类型"
                @change="dataTypeHandleChange"
              >
                <a-select-option value="原始数据">原始数据</a-select-option>
                <a-select-option value="特征值数据">特征值数据</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <template v-if="model.dataType == '特征值数据'">
            <a-col :span="24">
              <a-form-model-item label="采样间隔" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="timeInterval">
                 <a-input-number value="1" v-model="model.timeInterval" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="采样间隔单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="intervalUnit">
                <a-select default-value="分" v-model="model.intervalUnit" placeholder="请选择采样间隔单位">
                  <a-select-option value="分">分</a-select-option>
                  <a-select-option value="时">时</a-select-option>
                  <a-select-option value="天">天</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="聚合方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="method">
                <a-select default-value="平均值" v-model="model.method" placeholder="请选择聚合方式">
                  <a-select-option value="平均值">平均值</a-select-option>
                  <a-select-option value="方差">方差</a-select-option>
                  <a-select-option value="均方根">均方根</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </template>
          <a-col :span="24">
              <a-form-model-item label="批次样本数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="batchSize">
                <a-input v-model="model.batchSize" placeholder="请输入批次样本数量"></a-input>
              </a-form-model-item>
          </a-col>
          <a-col :span="24">
              <a-form-model-item label="训练次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="epochs">
                <a-input v-model="model.epochs" placeholder="请输入训练次数"></a-input>
              </a-form-model-item>
            </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <select-data-property-list-modal ref="selectDataPropertyList" @ok="selectDataPropertyOk" />
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { duplicateCheck } from '@/api/api'
import { validateDuplicateValue } from '@/utils/util'
import SelectDataPropertyListModal from '@/views/corp/dataProperty/SelectDataPropertyList'
export default {
  name: 'ModelBaseForm',
  components: {
    SelectDataPropertyListModal,
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      dataSetInfo:{},
      model: {
        name: '',
        type: 'LSTM',
        predictProperty: '',
        predictPropertyText:'',
        dataType: '原始数据',
        timeInterval:'1',
        intervalUnit: '分',
        method:'平均值',
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        code: [{ required: true, message: '请输入编号!' }],
        name: [
          { required: true, message: '请输入模型名称!' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' },
          { validator: this.validateName },
        ],
        type: [{ required: true, message: '请输入模型分类!' }],
        slotId: [{ required: true, message: '请输入槽号!' }],
        dataSetCode: [{ required: true, message: '请选择数据集!' }],
        predictProperty: [{ required: true, message: '请选择预测目标!' }],
        batchSize: [{ required: true, message: '请输入批次样本数量!' }],
        epochs: [{ required: true, message: '请输入训练次数!' }],
      },
      url: {
        add: '/modelBase/modelBase/add',
        edit: '/modelBase/modelBase/edit',
        queryById: '/modelBase/modelBase/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.$parent.confirmLoading = true
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok', res.result)
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    dataTypeHandleChange(value) {
      var that = this
      if (value == '原始数据') {
        that.model.timeInterval = '1'
        that.model.intervalUnit = '分'
        that.model.method = '平均值'
      }
    },
    validateName(rule, value, callback) {
      var params = {
        tableName: 'tb_model_base',
        fieldName: 'name',
        fieldVal: value,
        dataId: '',
      }
      duplicateCheck(params).then((res) => {
        if (res.success) {
          callback()
        } else {
          callback('名称已存在!')
        }
      })
    },
    doSelectDataPropertyList() {
      var type = ""
      if(this.dataSetInfo && this.dataSetInfo.dsType=='实时数据集'){
        type = "实时";
      }
      if(this.dataSetInfo && this.dataSetInfo.dsType=='日报数据集'){
        type = "日报";
      }
      if(type==''){
        this.$message.warning('请选选择数据集')
        return;
      }
      this.$refs.selectDataPropertyList.show(type)
    },
    selectDataPropertyOk(obj) {
      this.model.predictProperty = obj.propNo
      this.model.predictPropertyText = obj.propName
    },
    handleChangeDataSet(value){ 
      var that = this;
       getAction(`/dataSet/dataSet/queryByCode?code=`+value,{}).then(res=>{
          if (res.success) {
                that.dataSetInfo = res.result;
                that.model.dsType = that.dataSetInfo.dsType;//冗余 数据集类型（实时数据集、日报数据集）
          } else {
            console.error('queryByCode error: : ', res)
          }
        }) 
    }
  },
}
</script>